package cn.swu.ws.book;

import cn.swu.ws.dao.MysqlDbDao;

import cn.swu.ws.user.UserServlet;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;


import javax.print.DocFlavor;
import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
@WebServlet("/UpdateBook")
public class UpdateBook extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Connection connection ;
        try {
            connection = MysqlDbDao.login_database();
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
        Statement statement ;
        try {
            statement = connection.createStatement();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        Writer writer = resp.getWriter();
        String sql_tem = "select id,name,author,price,content from %s_book;";
        String sql = String.format(sql_tem, UserServlet.user_name);

        String first_template = "{" +
                "\"id\": \"%s\"," +
                "\"name\": \"%s\"," +
                "\"author\": \"%s\"," +
                "\"price\": %f," +
                "\"content\": \"%s\"" +
                "}";

        String template = ",{" +
                "\"id\": \"%s\"," +
                "\"name\": \"%s\"," +
                "\"author\": \"%s\"," +
                "\"price\": %f," +
                "\"content\": \"%s\"" +
                "}";
        /*String template = "'{\n" +
                "\"state\":true,\n" +
                "\"login_state\":true,\n" +
                "\"data\":[ \"Google\", \"Runoob\", \"Taobao\" ]\n" +
                "}'";*/
        writer.write("{\n" +
                "\"state\":true,\n" +
                "\"login_state\":true,\n" +
                "\"data\":[");
        try {
            ResultSet resultSet = statement.executeQuery(sql);
            int num = 0;
            while(resultSet.next()){
                num++;
               // System.out.println(resultSet.getString("name")+" "+resultSet.getString("author")+" "+resultSet.getString("price")+" "+resultSet.getString("content"));
                String id = resultSet.getString("id");
                String name = resultSet.getString("name");
                String author = resultSet.getString("author");
                float price = resultSet.getFloat("price");
                String content = resultSet.getString("content");
                if(num==1){
                    String book = String.format(first_template,id,name,author,price,content);
                    writer.write(book);
                }else{
                    String book = String.format(template,id,name,author,price,content);
                    writer.write(book);
                }
            }
            num =0;
        writer.write("]}");
            System.out.println(resp);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        doGet(req, resp);
    }
}
